<template>
	<view class="content">
		<view class="top">
			<image src="/static/img/bg-switch.png"></image>
			<text class="role">您当前身份是:{{ userInfo.memberRole == 'member' ? '求职者' : '招聘者' }}</text>
		</view>
		<view class="bottom">
			<button class="switch" @click="showDialog">切换为{{ userInfo.memberRole == 'member' ? '"招聘者"' : '"求职者"' }}身份</button>
			<button class="back" @click="back">返回</button>
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="warn" cancelText="关闭" :confirmText="confirmText" title="温馨提示"
				:content="dialogContent" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex';
	import IMService from '@/lib/imservice';
	export default {
		data() {
			return {
				confirmText: "确定",
				dialogContent: '~',
				roleName:'',
			};
		},
		computed: {
			...mapState(['userInfo']),
		},

		onLoad() {
			console.log("userinfo====",this.userInfo);
		},

		methods: {

			dialogClose() {
				console.log('点击关闭')
			},
			
			dialogConfirm(val) {
				console.log("dialogConfirm", val);
				this.$refs.alertDialog.close();
				this.switchRole();
			},

			showDialog() {
				let name= this.userInfo.memberRole == 'member' ? '"招聘者"': '"求职者"'
				this.dialogContent = "确定切换到" + name + "角色？";
				this.confirmText = "确定";
				this.$refs.alertDialog.open();
			},

			//角色切换
			async switchRole() {
				let role=this.userInfo.memberRole == 'member' ? 'company': 'member';
				let res = await this.$apis.switchRole({
					role: role,
				});
				console.log("==switchRole==", res);
				if (res.result) {
					uni.showToast({
						icon: 'none',
						title: '切换成功'
					});
					let permission = await this.$apis.getUserPermissionByToken();
					this.$store.commit('SET_TABBARS', permission.menu);
					this.$store.commit('SET_PERMISSION', permission.menu);
					let userInfo = await this.$apis.getUserInfo();
					this.$store.commit('SET_USERINFO', userInfo);
					if (this.goEasy.getConnectionStatus() === 'connected') {
						console.log('do  disconnect');
						//断开连接
						getApp().globalData.imService.disconnect();
						getApp().globalData.imService = new IMService(this.goEasy, this.GoEasy);
						getApp().globalData.imService.connect(userInfo);
					}
					uni.navigateBack();
				}
			}
		}
	};
</script>

<style lang="scss">
	.top {
		display: flex;
		flex-direction: column;
		align-items: center;

		image {
			width: 300px;
			height: 300px;
		}

		.role {
			font-size: $font-size-34;
			font-weight: bold;
		}
	}

	.bottom {
		margin-top: 50upx;

		.switch {
			width: 90%;
			color: $font-color-white;
			background-color: $main-color;
			font-size: $uni-font-size-lg;
		}

		.back {
			margin-top: 30upx;
			background-color: $bgcolor_white;
			width: 90%;
			border: 1upx solid $main-color;
			color: $main-color;
			font-size: $uni-font-size-lg;
		}
	}
</style>